using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Text;
using IQ.BaseWindows;
using IQ.Base;
using IQ.Base.Services;

namespace IQ.DbA
{
	/// <summary>
	/// Description of fmDefaults.
	/// </summary>
	public partial class FrmDefaults : Form
	{
		#region # Constructor # 
        public FrmDefaults()
		{
			InitializeComponent();
            selectColor.FullOpen = true;
		}
		#endregion
		
		#region - Private variables - 
		private DataTable Table = new DataTable();
        private ColorDialog selectColor = new ColorDialog();
		#endregion 

		#region - Private methods - 
        private void saveUserColors()
        {
            StringBuilder settings = new StringBuilder();
            settings.Append(FormSettings.FormBackColor.ToArgb() + ";"); //0
            settings.Append(FormSettings.ToolBarBackColor.ToArgb() + ";");//1
            settings.Append(FormSettings.LabelTextColor.ToArgb() + ";");
            settings.Append(FormSettings.InputBackColor.ToArgb() + ";");
            settings.Append(FormSettings.InputSelectedBackColor + ";");
            settings.Append(FormSettings.GridBackColor.ToArgb() + ";");
            settings.Append(FormSettings.GridAlternatingRowBC.ToArgb() + ";");
            settings.Append(FormSettings.GridColumnHeaderBC.ToArgb() + ";");
            settings.Append(FormSettings.GridRowHeaderBC.ToArgb() + ";");
            settings.Append(FormSettings.GridRowBC.ToArgb());
            Settings.SaveSetting("UserDefaults", settings.ToString());
        }
        private void loadUserColors()
        {
            FormSettings.SetThemeBlue();
            string userSet = Settings.ReadSettingString("UserDefaults", "");
            if (!String.IsNullOrEmpty(userSet))
            {
                string[] settings = userSet.Split(';');
                if (settings.Length == 10)
                {
                    FormSettings.SetColors(
                                        Color.FromArgb(settings[0].ToIntDefault(0)),
                                        Color.FromArgb(settings[1].ToIntDefault(0)),
                                        Color.FromArgb(settings[2].ToIntDefault(0)),
                                        Color.FromArgb(settings[3].ToIntDefault(0)),
                                        Color.FromArgb(settings[4].ToIntDefault(0)),
                                        Color.FromArgb(settings[5].ToIntDefault(0)),
                                        Color.FromArgb(settings[6].ToIntDefault(0)),
                                        Color.FromArgb(settings[7].ToIntDefault(0)),
                                        Color.FromArgb(settings[8].ToIntDefault(0)),
                                        Color.FromArgb(settings[9].ToIntDefault(0)));
                }
            }
            setColorFromDefault();
        }
        // Create dummy table for sample
        private void loadDataTable()
        {
            // Declare DataColumn and DataRow variables.
            DataColumn column;
            DataRow row;
            // Create new DataColumn, set DataType, ColumnName
            // and add to DataTable.    
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.Int32");
            column.ColumnName = "ID";
            Table.Columns.Add(column);
            // Create second column.
            column = new DataColumn();
            column.DataType = Type.GetType("System.String");
            column.ColumnName = "Description";
            Table.Columns.Add(column);
            // Create new DataRow objects and add to DataTable.    
            for (int i = 0; i < 6; i++)
            {
                row = Table.NewRow();
                row["ID"] = i;
                row["Description"] = "Sample line " + i;
                Table.Rows.Add(row);
            }
        }
        // Color changes
        private void changeFormColor()
        {
            FormSettings.FormBackColor = pnlFormBack.BackColor;
            this.BackColor = FormSettings.FormBackColor;
            pnlBaseM.BackColor = FormSettings.FormBackColor;
            changeControlColor();
        }
        private void changeControlColor()
        {
            gpbMain.BackColor = FormSettings.FormBackColor;
            gpbGrid.BackColor = FormSettings.FormBackColor;
            gpbText.BackColor = FormSettings.FormBackColor;
            gpbLastColor.BackColor = FormSettings.FormBackColor;
        }
        private void changeToolbarColor()
        {
            FormSettings.ToolBarBackColor = pnlToolBar.BackColor;
            tlsDefault.BackColor = FormSettings.ToolBarBackColor;
        }
        private void changeGridBackColor()
        {
            FormSettings.GridBackColor = pnlGridBack.BackColor;
            grdTable.BackgroundColor = FormSettings.GridBackColor;
        }
        private void changeTextColor()
        {
            FormSettings.LabelTextColor = pnlLabel.BackColor;
            FormSettings.InputBackColor = txtText.BackColor;
            FormSettings.InputSelectedBackColor = txtSelected.BackColor;
        }
        private void changeAllLabelsColor(Control ControlToLoop)
        {
            foreach (Control c in ControlToLoop.Controls)
            {
                Label lbl = (c as Label);
                if (lbl != null)
                {
                    lbl.ForeColor = FormSettings.LabelTextColor;
                }
                if (c.Controls.Count > 0)
                    changeAllLabelsColor(c);
            }
        }

        private DataGridViewCellStyle getStyleToChange()
        {
            switch (cbxSection.SelectedIndex)
            {
                case 0: return grdTable.AlternatingRowsDefaultCellStyle;  // Alternating rows
                case 1: return grdTable.ColumnHeadersDefaultCellStyle; // Column headers
                case 2: return grdTable.RowsDefaultCellStyle; // Default row / Normal row
                case 3: return grdTable.RowHeadersDefaultCellStyle; // Row headers (Row indicator)
                default: return null;
            }
        }
        private void setGridColorToDefault(Color newColor)
        {
            switch (cbxSection.SelectedIndex)
            {
                case 0: FormSettings.GridAlternatingRowBC = newColor; break;  // Alternating rows
                case 1: FormSettings.GridColumnHeaderBC = newColor; break; // Column headers
                case 2: FormSettings.GridRowBC = newColor; break; // Default row / Normal row
                case 3: FormSettings.GridRowHeaderBC = newColor; break; // Row headers (Row indicator)
            }
        }

        private void setColorFromDefault()
        {
            pnlFormBack.BackColor = FormSettings.FormBackColor;
            changeFormColor();
            pnlToolBar.BackColor = FormSettings.ToolBarBackColor;
            changeToolbarColor();
            //
            pnlLabel.BackColor = FormSettings.LabelTextColor;
            txtText.BackColor = FormSettings.InputBackColor;
            txtSelected.BackColor = FormSettings.InputSelectedBackColor;
            changeAllLabelsColor(this);
            // Grid Colors
            grdTable.BackgroundColor = FormSettings.GridBackColor;
            pnlGridBack.BackColor = FormSettings.GridBackColor;
            grdTable.AlternatingRowsDefaultCellStyle.BackColor = FormSettings.GridAlternatingRowBC;
            grdTable.ColumnHeadersDefaultCellStyle.BackColor = FormSettings.GridColumnHeaderBC;
            grdTable.RowsDefaultCellStyle.BackColor = FormSettings.GridRowBC;
            grdTable.RowHeadersDefaultCellStyle.BackColor = FormSettings.GridRowHeaderBC;
            //
            cbxSection.SelectedIndex = 0;
        }
        private void setLabelAndTextColors()
        {
            changeTextColor();
            changeAllLabelsColor(this);
        }

        #endregion 
		
		//#region - Public properties / variables - 
		//#endregion

		//#region - Public methods - 
		//#endregion
		
		#region - Bind Methods - 
		#endregion
		
		#region - Form Events -
		// This region is for event handler methods that are called without the user knowing about it.
		// Such as the page loading.
        private void fmDefaults_Load(object sender, EventArgs e)
        {
            loadDataTable();
            grdTable.DataSource = Table;
            grdTable.Columns[1].Width = 300;
            cbxSection.SelectedIndex = 0;
            loadUserColors();
            // Un set Defaults
            pnlSetGrd.BackColor = Color.Transparent;
            cbxThemes.FlatStyle = FlatStyle.Standard;
            txtText.Enter -= new EventHandler(General.Txt_Enter);
            txtText.Leave -= new EventHandler(General.Txt_Leave);
            txtSelected.Enter -= new EventHandler(General.Txt_Enter);
            txtSelected.Leave -= new EventHandler(General.Txt_Leave);
        }
        #endregion 
		
		#region - Control Methods -
		// This region is for event handler methods that are called knowingly by the user.
		// Such as a button being clicked.
        private void pnlcBack_Click(object sender, EventArgs e)
        {
            selectColor.Color = (sender as Control).BackColor;
            if (selectColor.ShowDialog() == DialogResult.OK)
            {
                txtLastColor.Text = selectColor.Color.ToArgb().ToString();
                TextBox txt = (sender as TextBox);
                if (txt != null)
                {
                    txt.BackColor = selectColor.Color;
                    setLabelAndTextColors();
                }
                else
                {
                    Panel pnl = (sender as Panel);
                    pnl.BackColor = selectColor.Color;
                    switch (pnl.Name)
                    {
                        case "pnlFormBack": changeFormColor(); break;
                        case "pnlToolBar": changeToolbarColor(); break;
                        case "pnlLabel": setLabelAndTextColors(); break;
                        case "pnlGridBack": changeGridBackColor(); break;
                        case "pnlGridRowBack": getStyleToChange().BackColor = selectColor.Color; setGridColorToDefault(selectColor.Color); break;
                        case "pnlGridRowFore": getStyleToChange().ForeColor = selectColor.Color; setGridColorToDefault(selectColor.Color); break;
                        case "pnlGridRowSBack": getStyleToChange().SelectionBackColor = selectColor.Color; setGridColorToDefault(selectColor.Color); break;
                        case "pnlGridRowSFore": getStyleToChange().SelectionForeColor = selectColor.Color; setGridColorToDefault(selectColor.Color); break;
                    }
                }
            }
        }

        private void lblFont_Click(object sender, EventArgs e)
        {
            FontDialog SelectFont = new FontDialog();
            if (SelectFont.ShowDialog() == DialogResult.OK)
            {
                lblFont.Font = SelectFont.Font;
                getStyleToChange().Font = SelectFont.Font;
            }
        }
        private void cbxSection_SelectedIndexChanged(object sender, EventArgs e)
        {
            System.Windows.Forms.DataGridViewCellStyle grdStyle = getStyleToChange();
            if (grdStyle != null)
            {
                pnlGridRowBack.BackColor = grdStyle.BackColor;
                pnlGridRowFore.BackColor = grdStyle.ForeColor;
                if (grdStyle.SelectionBackColor.IsEmpty)
                    pnlGridRowSBack.BackColor = Color.Empty;
                else
                    pnlGridRowSBack.BackColor = grdStyle.SelectionBackColor;
                pnlGridRowSFore.BackColor = grdStyle.SelectionForeColor;
            }
        }

        private void cbxThemes_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cbxThemes.SelectedIndex)
            {
                case 0: FormSettings.SetDefault(); break;//Default
                case 1: FormSettings.SetThemeBlue(); break;//Blue
                case 2: FormSettings.SetThemeGreen(); break;//Green
                case 3: FormSettings.SetThemeKhaki(); break;//Khaki
            }
            setColorFromDefault();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (General.MesDlg("Save current color scheme?", 'C') == DialogResult.Yes)
            {
                saveUserColors();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (General.MesDlg("Reset color scheme?", 'C') == DialogResult.Yes)
            {
                FormSettings.SetThemeBlue();
                setColorFromDefault();
                //fdUserSetting uSetting = fdUserSettingFactory.GetUserSetting(userId);
                //fdUserSettingFactory.DataDelete(uSetting);
            }
        }
        #endregion
    }
}
